import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:toast/toast.dart';
import 'package:loading_dialog/loading_dialog.dart';

class ConnectPage extends StatefulWidget {
  @override
  _ConnectPageState createState() => _ConnectPageState();
}

class _ConnectPageState extends State<ConnectPage> {
  static const platform = const MethodChannel('connect.flutter');
  String _message = '检查蓝牙状态中....';

  Future<void> _stateSearch() async {
    final String result = await platform
        .invokeMethod('idSearch', {'address': '38:BA:F8:89:00:B1'});
    print("======================");
    print(result);
  }

  Future<void> _connectState(context) async {
    LoadingDialog loading =
        LoadingDialog(buildContext: context, loadingMessage: '正在连接...');

    //        String deviceId = "20:20:03:27:31:74";"88:11:96:6A:14:29"; "E7:0C:2D:E1:F7:43";
    String result = await platform
        .invokeMethod('connectState', {'address': '88:11:96:6A:14:29'});
    print("_connectState");
    print(result);
    if (result == "404") {
      Toast.show("设备连接失败", context,
          duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
    } else {
      Toast.show("设备连接成功", context,
          duration: Toast.LENGTH_LONG, gravity: Toast.BOTTOM);
    }
  }

  Future<Null> _connect_input_stream() async {
    final String result = await platform.invokeMethod('inputStreamNotify');
    print("_connect_state");
    print(result);
    print("_connect_state");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Connect'),
      ),
      body: Container(
          child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text('蓝牙状态:'),
                Text(
                  _message,
                )
              ],
            ),
            Padding(
                padding: EdgeInsets.all(10.0),
                child: Column(
                  children: <Widget>[
                    RaisedButton(
                      color: Colors.blue,
                      textColor: Colors.white,
                      child: Text('搜索设备'),
                      onPressed: () {
                        _stateSearch();
                      },
                    ),
                    RaisedButton(
                      color: Colors.blue,
                      textColor: Colors.white,
                      child: Text('打开蓝牙'),
                      onPressed: () {
                        _connectState(context);
                      },
                    ),
                    RaisedButton(
                      color: Colors.blue,
                      textColor: Colors.white,
                      child: Text('获取数据'),
                      onPressed: () {
                        _connect_input_stream();
                      },
                    ),
                  ],
                ))
          ])),
    );
  }
}
